<?php
	    $max =$mysql->getVar("SELECT COUNT(*) as max FROM photo");
            $data = $mysql->getData("SELECT * FROM photo ORDER BY id desc"); //按ID倒序取的多维数组


            if($max==0)
            {
            $contentStr = "Sory  还没有人上传图片\n快点试试上传图片吧"; 
            $description="输入【菜单】返回     输入【相册】查看相册    输入【留言】给班长写信";
            $resultStr = sprintf($imageTpl, $fromUsername, $toUsername,$contentStr , $description,"","");  
            echo $resultStr;
            exit;
            }      
/*
  if ($max % 8) 
                {
                $PageCount = ( ($max - ($max % 8) ) / 8 ) + 1; //得到页码数
                }
            else{
                $PageCount = $max/8; //得到页码数
                }
                */
          $PageCount= floor(($max+7)/8 );  

 
		  if (is_numeric($form_Content))
    		{
                $PictureArray=$mysql->getLine("SELECT * FROM photo where id ='$form_Content';");
                if($PictureArray) 
                {
                $PictureID=$PictureArray['id'];
                $ticket = $PictureArray['ticket_number'] + 1;
                
                $mysql->runSql("UPDATE photo SET ticket_number = $ticket  
                    WHERE id='$form_Content'");
                $resultStr = sprintf($imageTpl, $fromUsername, $toUsername,
                        /* $title */ "投票成功 现在票数为:" . $ticket . "",
                        /* $Description */ "输入编号继续投票\n回复【菜单】回到主页\n回复【相册】查看图片\n回复【n】下一页图片",
                        /* $PicUrl */ $data[$add]['address'],
                        /* $url */ $data[$add]['address']);
                echo $resultStr;
                exit;
                }
            
                 if((!($PictureID)) ) 
                {
                
                $resultStr ="<xml>
                <ToUserName><![CDATA[$fromUsername]]></ToUserName>
                <FromUserName><![CDATA[$toUsername]]></FromUserName>
                <CreateTime>".time()."</CreateTime>
                <MsgType><![CDATA[news]]></MsgType>
                <ArticleCount>3</ArticleCount>
                <Articles>

				<item>
                <Title><![CDATA[╭(╯^╰)╮ 没有这个ID啊]]></Title>
                <Description><![CDATA[]]></Description>
                <PicUrl><![CDATA[http://p1.yokacdn.com/pic/fun/love/2011/U285P41T8D45525F231DT20110726100730.jpg]]></PicUrl>
                <Url><![CDATA[http://p1.yokacdn.com/pic/fun/love/2011/U285P41T8D45525F231DT20110726100730.jpg]]></Url>
                </item>

				<item>
				<Title><![CDATA[我们找了很久了，真的找不到！！\no(>﹏<)o。\n求求您检测一下正确的ID 好吗? \n(⊙_⊙)\n回复【n】或【N】查看下一页\nO(∩_∩)O]]></Title> 
                <Description><![CDATA[]]></Description>
                <PicUrl><![CDATA[]]></PicUrl>
                <Url><![CDATA[]]></Url>
                </item>
                
                
                <item>
				<Title><![CDATA[输入【菜单】返回  输入【相册】【通知】【mbox】【留言】进入相应功能]]></Title> 
                <Description><![CDATA[]]></Description>
                <PicUrl><![CDATA[]]></PicUrl>
                <Url><![CDATA[]]></Url>
                </item>

				</Articles>
           		<FuncFlag>0</FuncFlag>
           		</xml> ";
                echo $resultStr;
                exit; 
                }
                
   		}
           

            if ($form_Content == "相册")
            {
                $mc->delete($fromUsername . "_page", 0);
                $mc->set($fromUsername . "_do", "相册", 0, 600);
                $mc->set($fromUsername . "_page", "0", 0, 600);
            }
            $LastPage = $mc->get($fromUsername . "_page");

            if ((($form_Content == "n") || ($form_Content == "N")) && $PageCount == $LastPage)
            {
                $resultStr = sprintf($imageTpl, $fromUsername, $toUsername, /* $title */ "上面的已经是最后一页了", /* $Description */ "不要在往下翻啦！快点投票吧", /* $PicUrl */ "http://i5.3conline.com/images/piclib/201002/12/batch/1/55243/1265965093578mue0odu9ma.jpg", /* $url */ "http://i5.3conline.com/images/piclib/201002/12/batch/1/55243/1265965093578mue0odu9ma.jpg");
                echo $resultStr;
                exit();
            }
            if (($form_Content == "相册") && $LastPage == 0)
                $mc->set($fromUsername . "_page", "1", 0, 600);
            if ((($form_Content == "n") || ($form_Content == "N")) && $LastPage != 0)
                $mc->set($fromUsername . "_page", "".($LastPage+1)."", 0, 600);
            $NowPage = $mc->get($fromUsername . "_page");


            if ($NowPage == $PageCount) 
                if(!($max % 8))
                	$ArticleCount = 8;
                else
                	$ArticleCount = $max % 8;
            if ($NowPage != $PageCount)
                $ArticleCount = 8;
          
            $resultStr = 
                	"<xml>
                    <ToUserName> <![CDATA[$fromUsername]]></ToUserName>
                    <FromUserName><![CDATA[$toUsername]]></FromUserName>
                    <CreateTime>" . time() . "</CreateTime>
                    <MsgType><![CDATA[news]]></MsgType>
                    <ArticleCount>" . ($ArticleCount + 2) . "</ArticleCount>
                    <Articles>   ";
            
            $resultStr .= 
                	"<item>
					<Title><![CDATA[四班相册(输入id号直接投票)]]> </Title> 
					<Description> <![CDATA[]]></Description>
          			<PicUrl>
           				<![CDATA[http://scnuzs-weixincourse.stor.sinaapp.com/cover2%20.jpg]]></PicUrl>
          			<Url>
          				<![CDATA[]]></Url>
          			</item>";
	
            for ($times = (($LastPage * 8) + 0); $times < (($LastPage * 8) + $ArticleCount); $times++) 
            {
              
                $resultStr .=
          			"<item>
          			<Title><![CDATA[☆☆图片编号——". $data[$times]['id']."\n☆☆作者: ".$data[$times]['student_name']."\n☆☆现在的票数是：". $data[$times]['ticket_number']."\n↖(^ω^)↗快投我一票哦 !]]> </Title> 
          			<Description> <![CDATA[]]></Description>
          			<PicUrl>
          			<![CDATA[" . $data[$times]['address'] . "]]></PicUrl>
          			<Url>
          			<![CDATA[" . $data[$times]['address'] . "]]></Url>
          			</item>";
            }

            $resultStr .= "
            <item>";
            if ($ArticleCount == 8)
                $resultStr .= " <Title><![CDATA[现在是第:" . $NowPage . "页  共".$PageCount."页\n下一页（回复【N】）\n回复【菜单】返回主页]]> </Title>";
            else
                $resultStr .= " <Title><![CDATA[现在是最后一页了！]]> </Title>";
            $resultStr .= 
          			"<Description> <![CDATA[]]></Description>
          			<PicUrl><![CDATA[]]></PicUrl>
          			<Url><![CDATA[]]></Url>
          			</item>";

            $resultStr .= "
          			</Articles><FuncFlag>0</FuncFlag></xml>";
            echo $resultStr;
            exit;






?>